package com.aidex.quartz.controller;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.quartz.domain.SysJobLog;
import com.aidex.quartz.service.SysJobLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Api(tags = "调度日志操作处理")
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController extends BaseController<SysJobLogService> {

    @ApiOperation("查询定时任务调度日志列表")
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public R list(SysJobLog jobLog,
                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                       HttpServletRequest req) {
        QueryWrapper<SysJobLog> queryWrapper = QueryGenerator.initQueryWrapper(jobLog, req.getParameterMap());
        Page<SysJobLog> page = new Page<>(pageNo, pageSize);
        IPage<SysJobLog> pageList = service.selectPage(page, queryWrapper);
        return R.data(pageList);
    }

    @ApiOperation("导出定时任务调度日志列表")
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @Log(title = "任务调度日志" , businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SysJobLog jobLog, HttpServletRequest req) {
        QueryWrapper<SysJobLog> queryWrapper = QueryGenerator.initQueryWrapper(jobLog, req.getParameterMap());
        List<SysJobLog> list = service.selectList(queryWrapper);
        ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
        return util.exportExcel(null, "调度日志");
    }

    /**
     * 根据调度编号获取详细信息
     */
    @ApiOperation("根据调度编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable Long id) {
        return R.data(service.selectById(id));
    }


    @ApiOperation("删除定时任务调度日志")
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务调度日志" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobLogIds}")
    public R remove(@PathVariable Long[] jobLogIds) {
        return toAjax(service.deleteBatchIds(Arrays.asList(jobLogIds)));
    }

    @ApiOperation("清空定时任务调度日志")
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "清空调度日志" , businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R clean() {
        service.clean();
        return R.success();
    }
}
